package com.example.gps;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.GpsSatellite;
import android.location.GpsStatus;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import java.util.Iterator;

public class MainActivity extends AppCompatActivity {

    private static LocationManager locationManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 999);
        }
        locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
        locationManager.addGpsStatusListener(new GpsStatus.Listener() {
            @Override
            public void onGpsStatusChanged(int event) {
                switch (event){
                    case GpsStatus.GPS_EVENT_STARTED:
                        Toast.makeText(MainActivity.this,"GPS_EVENT_STARTED",Toast.LENGTH_SHORT).show();
                        break;
                    case GpsStatus.GPS_EVENT_STOPPED:
                        Toast.makeText(MainActivity.this,"GPS_EVENT_STOPPED",Toast.LENGTH_SHORT).show();
                        break;
                    case GpsStatus.GPS_EVENT_FIRST_FIX:
                        Toast.makeText(MainActivity.this,"GPS_EVENT_FIRST_FIX",Toast.LENGTH_SHORT).show();
                        break;
                    case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
                        @SuppressLint("MissingPermission")
                        GpsStatus gpsStatus = locationManager.getGpsStatus(null);
                        int maxSatellites=gpsStatus.getMaxSatellites();
                        Iterator<GpsSatellite> gpsSatelliteIterator = gpsStatus.getSatellites().iterator();
                        int count=0;
                        while (gpsSatelliteIterator.hasNext()&&count<maxSatellites){
                            count++;
                            GpsSatellite gpsSatellite = gpsSatelliteIterator.next();
                            float snr = gpsSatellite.getSnr();
                            if(snr>30){
                                Toast.makeText(MainActivity.this,count+":"+snr,Toast.LENGTH_SHORT).show();
                            }
                        }
                        break;
                }
            }
        });
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        for (int i = 0; i < permissions.length; i++) {
            Log.e("androidLog", requestCode + "==" + permissions[i] + "==" + grantResults[i]);
        }
    }

    public void btnOpenGps(View view) {
        if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(intent);
        }
    }

    public void btnCloseGps(View view) {
        if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(intent);
        }
    }

    @SuppressLint("MissingPermission")
    public void btnStartLocation(View view) {

        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                Toast.makeText(MainActivity.this,location.getLatitude()+":"+location.getLongitude(),Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {

            }

            @Override
            public void onProviderEnabled(String provider) {
                Toast.makeText(MainActivity.this,"onProviderEnabled",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onProviderDisabled(String provider) {
                Toast.makeText(MainActivity.this,"onProviderDisabled",Toast.LENGTH_SHORT).show();
            }
        });
    }
}


